package cn.shengchao.rule.engine;

import cn.hutool.core.collection.CollUtil;
import cn.shengchao.rule.base.AssignInviteAction;
import cn.shengchao.rule.base.ConditionGroup;
import cn.shengchao.rule.base.IAction;
import cn.shengchao.rule.base.SpecificUserAction;
import cn.shengchao.rule.condition.EntryUriCondition;
import cn.shengchao.rule.condition.ICondition;
import cn.shengchao.rule.condition.RegisterDateRangeCondition;
import cn.shengchao.rule.condition.paramKey.IConditionParamKey;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @program: examstarapi
 * @description: 123
 * @author: Xiong weiqiang
 * @create: 2025-04-07 21:11
 */
public class Main {
    public static void main(String[] args) {
        // 初始化规则管理器并加载规则
//        RuleManager ruleManager = new RuleManager();
//        ruleManager.loadRules();
        Rule rule = new Rule();
        rule.setId(1L);
        rule.setName("VIP用户优惠券规则");
        rule.setPriority(1);
        rule.setStartTime(LocalDateTime.now());
        rule.setEndTime(LocalDateTime.MAX);
        ConditionGroup conditionGroup = new ConditionGroup();
        EntryUriCondition entryUriCondition = new EntryUriCondition();
        entryUriCondition.setMatchValue("1");
        RegisterDateRangeCondition registerDateRangeCondition = new RegisterDateRangeCondition();
        registerDateRangeCondition.setMatchValue("20240102,20250201");
        List<ICondition> conditionList=Arrays.asList(entryUriCondition,registerDateRangeCondition);
        //优先级小的排前面
        CollUtil.sort(conditionList, Comparator.comparingInt(ICondition::getPriority));
        conditionGroup.setConditions(conditionList);
        conditionGroup.setAction(new AssignInviteAction());

        ConditionGroup conditionGroup1 = new ConditionGroup();
        EntryUriCondition entryUriCondition1 = new EntryUriCondition();
        entryUriCondition1.setMatchValue("2");
        RegisterDateRangeCondition registerDateRangeCondition1 = new RegisterDateRangeCondition();
        registerDateRangeCondition1.setMatchValue("20250201,20250501");
        List<ICondition> conditionGroupList1=Arrays.asList(entryUriCondition1,registerDateRangeCondition1);
        //优先级小的排前面
        conditionGroupList1.sort(Comparator.comparingInt(ICondition::getPriority));
        conditionGroup1.setConditions(conditionGroupList1);
        conditionGroup1.setAction(new SpecificUserAction());
        List<ConditionGroup> conditionGroupList=Arrays.asList(conditionGroup,conditionGroup1);
        rule.setConditionGroups(conditionGroupList);
        List<IRule> rules = new ArrayList<>();
        rules.add(rule);
        rules.sort(Comparator.comparingInt(IRule::getPriority));
        // 初始化规则引擎
        RuleEngine.cleanAndLoadRules(rules);

        // 模拟输入数据
        Map<String,String> inputData = new HashMap<>();
        inputData.put(IConditionParamKey.ENTRY_URI,"2");
        inputData.put(IConditionParamKey.REGISTER_DATE_RANGE, "20250201");
        inputData.put("商品ID", "12345");
        // 执行规则
        List<ConditionGroup> conditionGroup2=RuleEngine.executeRules(inputData);
        IAction action = conditionGroup2.getFirst().getAction();
        System.out.println("执行类型：" + action.getActionType()+",执行目标"+ action.getActionValue());
    }
}